class StopInstancesController < ApplicationController
  def set_visited
    @stop_instance = StopInstance.find(params["id"])
    @stop_instance.visited = true	
    @stop_instance.save
    respond_to do |format|
      format.json  { render :json => [@stop_instance] }
    end		
  end

  # GET /stop_instances
  # GET /stop_instances.json
  def index
    @stop_instances = StopInstance.all

    respond_to do |format|
      format.html # index.html.erb
      format.json  { render :json => @stop_instances }
    end
  end

  # GET /stop_instances/1
  # GET /stop_instances/1.json
  def show
    @stop_instance = StopInstance.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json  { render :json => @stop_instance }
    end
  end

  # GET /stop_instances/new
  # GET /stop_instances/new.json
  def new
    @stop_instance = StopInstance.new

    respond_to do |format|
      format.html # new.html.erb
      format.json  { render :json => @stop_instance }
    end
  end

  # GET /stop_instances/1/edit
  def edit
    @stop_instance = StopInstance.find(params[:id])
  end

  # POST /stop_instances
  # POST /stop_instances.json
  def create
    if request.content_type == "application/json"
      @stop_instance = StopInstance.new(
        :visited => params["visited"],
        :stop_id => params["stop_id"],
        :location_id => params["location_id"],
        :stop_number => params["stop_number"]
      )
    else
      @stop_instance = StopInstance.new(params[:stop_instance])
    end
    # lazily instantiate route_instances for API
    # move to model?
    # this will be buggy around midnight (double creates route_instances)
    unless @stop_instance.route_instance
      if @stop_instance.stop
        ri = RouteInstance.find_by_day_and_courier_id_and_route_id(Date.today, 
        @current_user.courier.id, @stop_instance.stop.route.id)
        if ri
          @stop_instance.route_instance = ri
        else
          @stop_instance.route_instance = RouteInstance.new(
            :day => Date.today, :courier => @current_user.courier,
            :route => @stop_instance.stop.route
          )
          @new_stops = []
          @stop_instance.route_instance.route.stops.each { |x|
            unless x.location_id == params["location_id"]
              @s = StopInstance.create(
                :location_id => x.location_id,
                :route_instance => @stop_instance.route_instance,
                :stop_number => x.stop_number,
                :visited => false,
                :location_id => x.location_id
              )
              @new_stops << @s
              @s.save
            end
          }
          @stop_instance.route_instance.save
        end
      end
    end
    
    respond_to do |format|
      if @stop_instance.save
        flash[:notice] = 'StopInstance was successfully created.'
        format.html { redirect_to(@stop_instance) }
        format.json  { render :json => @stop_instance, :status => :created, :location => @stop_instance }
      else
        format.html { render :action => "new" }
        format.json  { render :json => @stop_instance.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /stop_instances/1
  # PUT /stop_instances/1.json
  def update
    @stop_instance = StopInstance.find(params[:id])

    respond_to do |format|
      if @stop_instance.update_attributes(params[:stop_instance])
        flash[:notice] = 'StopInstance was successfully updated.'
        format.html { redirect_to(@stop_instance) }
        format.json  { head :ok }
      else
        format.html { render :action => "edit" }
        format.json  { render :json => @stop_instance.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /stop_instances/1
  # DELETE /stop_instances/1.json
  def destroy
    @stop_instance = StopInstance.find(params[:id])
    @stop_instance.destroy

    respond_to do |format|
      format.html { redirect_to(stop_instances_url) }
      format.json  { head :ok }
    end
  end
end
